<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-adx__account}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.name" placeholder="账户名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="运营负责人">
            <el-select v-model="dataForm.userName" placeholder="选择运营人员" filterable clearable>
              <el-option v-for="item in userNameList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="商务">
            <el-select v-model="dataForm.business" placeholder="选择运营人员" filterable clearable>
              <el-option v-for="item in businessList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>

                 <el-form-item label="所属产品" prop="productChannel">
           <el-select v-model="dataForm.productChannel" placeholder="选择运营人员" filterable clearable>
              <el-option v-for="item in productList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>
         <el-form-item label="所属媒体" prop="mediaChannel">
         <el-select v-model="dataForm.mediaChannel" placeholder="选择运营人员" filterable clearable>
              <el-option v-for="item in mediaList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>


        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('adx:account:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('adx:account:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="id" label="广告主ID" :show-overflow-tooltip='true' header-align="center" align="center"></el-table-column>
        <el-table-column prop="name"    label="名称" header-align="center" align="center"></el-table-column>
       
         <el-table-column prop="userName" width="100" label="用户名称" header-align="center" align="center"></el-table-column>
        <el-table-column prop="business" width="100" label="所属商务" header-align="center" align="center"></el-table-column>
        <el-table-column prop="per" label="回传比例" header-align="center" align="center">
           <template slot-scope="scope">
               <el-progress :text-inside="true" :stroke-width="26" :percentage="scope.row.per*100"></el-progress>
            </template>
        </el-table-column>
        <el-table-column prop="keyType" label="结算指标" header-align="center" align="center">
           <template slot-scope="scope">
            {{ $getDictLabel("typeList", scope.row.keyType) }}
          </template>
        </el-table-column>
         <el-table-column prop="dayLimitNumber"    label="当日限量" header-align="center" align="center"></el-table-column>
        <el-table-column prop="totalLimitNumber"   label="总数限量" header-align="center" align="center"></el-table-column>
         <el-table-column prop="status"    label="账户状态" header-align="center" align="center">
           <template slot-scope="scope">
              <el-tag v-if="scope.row.status==1" type="success">{{  $getDictLabel("account_status", scope.row.status)}}</el-tag>
              <el-tag v-if="scope.row.status==2" type="info">{{  $getDictLabel("account_status", scope.row.status)}}</el-tag>
              <el-tag v-if="scope.row.status==3" type="danger">{{  $getDictLabel("account_status", scope.row.status)}}</el-tag>
            </template>
         </el-table-column>
        <!-- <el-table-column prop="createTime" label="创建时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" header-align="center" align="center"></el-table-column> -->
        <el-table-column prop="productChannel" width="100" label="产品" header-align="center" align="center">
           <template slot-scope="scope">
            {{ $getDictLabel("productList", scope.row.productChannel) }}
          </template>
        </el-table-column>
        <el-table-column prop="mediaChannel" width="100" label="媒体" header-align="center" align="center">
               <template slot-scope="scope">
            {{ $getDictLabel("mediaList", scope.row.mediaChannel) }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="userName" width="100" label="用户名称" header-align="center" align="center"></el-table-column>
        <el-table-column prop="business" width="100" label="所属商务" header-align="center" align="center"></el-table-column> -->
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" >
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('adx:account:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('adx:account:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
            <!-- <el-button  type="text" size="small" @click="generate(scope.row.id,scope.row.templateId)">生成监测链接</el-button> -->
            <el-button  type="text" size="small" @click="copyAccount(scope.row.id)">复制账户</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './account-add-or-update'
import Copy from 'copy-to-clipboard';
import { getDictDataList } from '@/utils'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/adx/account/page',
        getDataListIsPage: true,
        exportURL: '/adx/account/export',
        deleteURL: '/adx/account',
        deleteIsBatch: true
      },
      userNameList: [],
      businessList:[],
         mediaList:[],
      productList:[],
      dataForm: {
        id: ''
      },
      dataForm:{}
    }
  },
    mounted() {
    this.getNameDict();
    this.getBusinessDict();
        this.getProductList();
    this.getMediaList();
  },
  components: {
    AddOrUpdate
  },
  methods:{
    generate(id,templateId){
      this.dataForm.id=id;
      this.dataForm.templateId=templateId;
      if(!templateId){
        this.$message.error("未配置监测联调模板，请先配置模板")
        return;
      }
      return this.$http.get('/adx/account/generateUrl',{params:this.dataForm}).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        Copy(res.data);  
        this.$message('监测链接生成成功，已复制在粘贴板')
      }).catch(() => { })
    },
    copyAccount(id){
      //复制账户的功能
      this.$confirm('确认要复制当前账户吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
               return this.$http.get('/adx/account/copy?id='+id).then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.getDataList()
              this.$message('账户复制成功')
               })
          }).catch(() => {
                   
        });
    },
    getNameDict() {
      this.userNameList = getDictDataList("userNameList")

    },
    getBusinessDict() {
      this.businessList = getDictDataList("businessList")
    },
           getMediaList() {
      this.mediaList = getDictDataList("mediaList")
    },
       getProductList() {
      this.productList = getDictDataList("productList")
    },
  }
}
</script>
